<?php

namespace api\wechat;

use utils\base64_to_file;
use utils\oss;
use utils\redis;
use utils\wechat_pay;

class driver
{
    //获取分页列表
    public function getList($data)
    {
        $page = (int)$data['page'];
        $size = (int)$data['size'];
        $openid = $data['openid'];
        $start = ($page - 1) * $size;
        $time = time();

        $con = con();
        $start_time = strtotime(date('Y-m-d'));
        $end_time = $start_time + 86399;

        //检测用户是否存在，不存在则自动创建
        $userinfo = (new user())->getUserInfo($openid);
        $user_id = (int)$userinfo['id'];

        $where = " where is_delete=1 ";
        $limit = " order by `id` desc limit $start,$size";
        $sql = "select * from driver $where";
        $data = mysqli_query($con, $sql . $limit);
        $data = mysqli_fetch_all($data, true);
        $total = mysqli_query($con, $sql);
        $total = mysqli_num_rows($total);

        //匹配驾驶员和用户的评价数据
        $comment = mysqli_query($con, "select * from driver_record where user_id=$user_id and (create_time between $start_time and $end_time)");
        $comment = mysqli_fetch_all($comment, true);
        for ($i = 0; $i < count($data); $i++) {
            $data[$i]['has_comment'] = false;
            $data[$i]['comment'] = [];
            $data[$i]['has_comment_text'] = '今日未评价';
            for ($o = 0; $o < count($comment); $o++) {
                if ($data[$i]['id'] === $comment[$o]['driver_id']) {

                    if (empty($comment[$o]['img_url'])) {
                        $comment[$o]['img_url'] = [];
                    } else {
                        $comment[$o]['img_url'] = explode(',', $comment[$o]['img_url']);
                    }
                    $comment[$o]['type']=(int)$comment[$o]['type'];
                    $data[$i]['has_comment'] = true;
                    $data[$i]['comment'] = $comment[$o];
                    $data[$i]['has_comment_text'] = '今日已评价';
                }
            }
        }

        mysqli_close($con);
        return out_right([
            'data' => $data,
            'page' => $page,
            'size' => $size,
            'total' => $total,
            'total_page' => ceil($total / $size)
        ]);
    }

    //驾驶员评价内容上传
    public function up($data)
    {
        $con = con();
        $user_id = (int)$data['user_id'];
        $driver_id = (int)$data['driver_id'];
        $type = (int)$data['type'];
        $img = $data['img'];
        $content = $data['content'];
        $date = date('Y-m-d');
        $time = time();
        $url=[];
        for ($i = 0; $i < count($img); $i++) {
            $file = base64_to_file::get("", $img[$i]["file_name"], $img[$i]["file"]);
            $res = oss::upload("driver/$date/" . $file["name"], $file["path"]);
            $url[] = $res["info"]["url"];
        }
        if(empty($url)){
            $url='';
        }else{
            $url=implode(',', $url);
        }
        $res = mysqli_query($con, "insert into driver_record(user_id,driver_id,type,img_url,content,create_time,update_time) values($user_id,$driver_id,$type,'$url','$content',$time,$time)");
        mysqli_close($con);
        if ($res) {
            return out_right('评价成功！');
        } else {
            return out_error('评价失败！');
        }
    }

    //驾驶员评价详情页面初始化（根据openid检测用户是否存在，不存在则自动创建，并索引相关评论数据）
    public function init($data)
    {
        $user=new user();
        $res=$user->login($data);
        $res=json_decode($res,true);
        if($res['code']!==200){
            return json_encode($res);
        }
        $token = $res['data']['token'];
        $openid = $res['data']['openid'];
        $driver_id = (int)$data['driver_id'];
        $con = con();
        $start_time = strtotime(date('Y-m-d'));
        $end_time = $start_time + 86399;
        $userinfo = $user->getUserInfo($openid);
        $user_id = (int)$userinfo['id'];
        $driver = mysqli_query($con, "select * from driver where id=$driver_id and is_delete=1 and status=1");
        $driver = mysqli_fetch_all($driver, true);
        if(count($driver)===0){
            mysqli_close($con);
            return out_error('此驾驶员已被删除或禁用！');
        }
        $driver = $driver[0];

        //匹配驾驶员和用户的评价数据
        $comment = mysqli_query($con, "select * from driver_record where user_id=$user_id and (create_time between $start_time and $end_time)");
        $comment = mysqli_fetch_all($comment, true);
        $driver['has_comment'] = false;
        $driver['comment'] = [];
        $driver['has_comment_text'] = '今日未评价';
        for ($o = 0; $o < count($comment); $o++) {
            if ($driver['id'] === $comment[$o]['driver_id']) {

                if (empty($comment[$o]['img_url'])) {
                    $comment[$o]['img_url'] = [];
                } else {
                    $comment[$o]['img_url'] = explode(',', $comment[$o]['img_url']);
                }

                $driver['has_comment'] = true;
                $driver['comment'] = $comment[$o];
                $driver['has_comment_text'] = '今日已评价';
            }
        }
        mysqli_close($con);
        $driver['token'] = $token;
        $driver['openid'] = $openid;
        $driver['userinfo']=$userinfo;
        return out_right($driver);
    }

    //测试
    public function test(){
        $path="pages/menu/driver/info/index";
        $scene='driver_id=666';
        $url=getWechatQrcode($scene,$path);
        return $url;
    }

}